package com.ww.word.ui;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.ww.core.activity.SlideMenuActivity;
import com.ww.core.widget.scroll.PullToRefreshView;
import com.ww.core.widget.scroll.PullToRefreshView.OnFooterRefreshListener;
import com.ww.word.R;
import com.ww.word.adapter.StoreAdapter;
import com.ww.word.db.StudyDbHelper;
import com.ww.word.entity.Word;
import com.ww.word.ui.menu.LeftFragment;

public class StoreActivity extends SlideMenuActivity implements OnFooterRefreshListener {
	private GridView grid;
	private List<Word> list;
	private StoreAdapter storeAdapter;
	private PullToRefreshView mPullToRefreshView;
	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_store, container, false);
		initView(view);
		return view;
	}

	private void initView(View view) {
		setTitle(view, LeftFragment.mTitleName);
		grid = (GridView) view.findViewById(R.id.grid);
		mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.main_pull_refresh_view);
		mPullToRefreshView.setOnFooterRefreshListener(this);
		mPullToRefreshView.hideHeader();
		getStore();

		IntentFilter myIntentFilter = new IntentFilter();
		myIntentFilter.addAction("store");
		context.registerReceiver(mBroadcastReceiver, myIntentFilter);
	}

	private void getStore() {
		showProgressDialog(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				StudyDbHelper w = new StudyDbHelper(context);
				list = w.getStoreList(null);
				if (list.size() < 40) {
					handler.sendEmptyMessage(2);
				}
				handler.sendEmptyMessage(1);
			}
		}).start();
	}

	// 定义一个Handler，用来异步处理数据
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				if (list.size() > 0) {
					if (storeAdapter == null) {
						storeAdapter = new StoreAdapter(context, list);
						grid.setAdapter(storeAdapter);
					} else {
						storeAdapter.setList(list);
						storeAdapter.notifyDataSetChanged();
					}
					closeFailView(view);
				} else {
					failView(view, context.getString(R.string.store_empty), R.drawable.list_empty_icon);
				}

				grid.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
						Intent i = new Intent(context, ResultActivity.class);
						i.putExtra("from", "store");
						i.putExtra("index", arg2);
						i.putExtra("wordList", list.toArray());
						startActivity(i);
					}
				});
				break;
			case 2:
				mPullToRefreshView.hideFooter();
				break;
			default:
				break;
			}
			mPullToRefreshView.onFooterRefreshComplete();
			hideProgressDialog();
		};
	};

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				StudyDbHelper w = new StudyDbHelper(context);
				List<Word> temp = w.getStoreList(list.get(list.size() - 1).getTime());
				list.addAll(temp);
				if (temp.size() < 40) {
					handler.sendEmptyMessage(2);
				}
				handler.sendEmptyMessage(1);
			}
		}).start();
	}

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			getStore();
		}
	};

}
